package com.neiquan.backstage.platform_article.web.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.platform_article.PlatformArticleTypeService;
import com.neiquan.backstage.pub.bsc.dao.po.PlatformArticleType;

@Controller
@RequestMapping(value = "platformArticleType", produces = { "text/json;charset=UTF-8" })
public class PlatformArticleTypeAction {
	private static Class<?> poc = PlatformArticleType.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private PlatformArticleTypeService articleTypeService;

	@ResponseBody
	@RequestMapping("queryAll")
	public String queryAll() {
		return CommonResp.buildSuccessResp(articleTypeService.queryAll()).toJsonString();
	}

	@ResponseBody
	@RequestMapping("list")
	public String list(int pageIndex, int pageSize) {
		Paging<?> paging = extraSpringHibernateTemplate.findPage(poc, Order.desc("articleTypeId"), pageIndex * pageSize, pageSize);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String name, String dtCreatBegin, String dtCreatEnd) throws ParseException {
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(name)) {
			criterions.add(Restrictions.like("name", "%" + name + "%"));
		}
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		if (StringUtils.isNoneBlank(dtCreatBegin)) {
			criterions.add(Restrictions.ge("dtCreat", dateFormat.parse(dtCreatBegin).getTime()));
		}
		if (StringUtils.isNoneBlank(dtCreatEnd)) {
			criterions.add(Restrictions.le("dtCreat", dateFormat.parse(dtCreatEnd).getTime()));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("articleTypeId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "remove")
	public String remove(Integer id) {
		PlatformArticleType toDelete = articleTypeService.queryById(id);
		if (toDelete == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(toDelete);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "add")
	public String add(String name) {
		if (articleTypeService.exists(name)) {
			return CommonResp.buildErrorResp(BackendErrorCode.already_exist).toJsonString();
		}
		articleTypeService.add(name);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
